/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bbdd;

import java.sql.*;


/**
 * Clase que almacena constantes y instancias de la base de datos.
 * @author JAVA
 */
public class Persistencia {

    public final static String HOST = "localhost";
    public final static String BD_NAME = "subana";
    public final static String USER = "root";
    public final static String PASSWORD = "root";
    public final static String INSTANCE = "jdbc:mysql://" + HOST + "/" + BD_NAME;
    public final static String DRIVER = "com.mysql.jdbc.Driver";

    /**
     * Devuelve la conexión a la base de datos, lista para indicarle una sentencia SQL
     * @return
     * @throws ClassNotFoundException
     * @throws InstantiationException
     * @throws IllegalAccessException
     * @throws SQLException 
     */
    public static Statement getConnection() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
        Class.forName(DRIVER).newInstance();
        Connection con = DriverManager.getConnection(INSTANCE, USER, PASSWORD);
        Statement st = con.createStatement();
        return st;
    }

    /**
     * Cierra el Statement y la conexión asociada
     * @param st Statement
     * @throws SQLException 
     */
    public static void closeConnection(Statement st) {
        Connection con = null;
        try {
            con = st.getConnection();
        } catch (SQLException e) {
        } finally {
            try {
                if (con != null) {
                    con.close();
                }
                if (st != null) {
                    st.close();
                }
            } catch (SQLException e) {
            }
        }
    }
}
